﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class tab_newDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="nid">标识列ID</param>
		public bool Exists(int nid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [tab_new] where nid=@nid ");
			htSyn.Add("@nid", nid);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<tab_new> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [createDate],[nid],[newInfo],[newTitle],[newKey],[newDescription],[newName] FROM [tab_new]");
			 return gettab_newBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="nid">标识列ID</param>
		/// <returns></returns>
		public tab_new GetModel(int nid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [createDate],[nid],[newInfo],[newTitle],[newKey],[newDescription],[newName] FROM [tab_new]");
			strSql.Append(" where nid=@nid ");
			htSyn.Add("@nid", nid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			tab_new tab_newModel = new tab_new();
			if (dt.Rows.Count > 0)
			{
				tab_newModel.createDate=Convert.ToDateTime(dt.Rows[0]["createDate"]);
				tab_newModel.newInfo=dt.Rows[0]["newInfo"].ToString();
				tab_newModel.newTitle=dt.Rows[0]["newTitle"].ToString();
				tab_newModel.newKey=dt.Rows[0]["newKey"].ToString();
				tab_newModel.newDescription=dt.Rows[0]["newDescription"].ToString();
				tab_newModel.newName=dt.Rows[0]["newName"].ToString();
			}
			return tab_newModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="tab_new">实体</param>
		/// <returns></returns>
		public bool Update(tab_new tab_newModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [tab_new] set ");
			strSql.Append("[createDate]=@createdate,");
			strSql.Append("[newInfo]=@newinfo,");
			strSql.Append("[newTitle]=@newtitle,");
			strSql.Append("[newKey]=@newkey,");
			strSql.Append("[newDescription]=@newdescription,");
			strSql.Append("[newName]=@newname");

			strSql.Append(" where nid=@nid ");
			htSyn.Add("@createdate", tab_newModel.createDate);
			htSyn.Add("@nid", tab_newModel.nid);
			htSyn.Add("@newinfo", tab_newModel.newInfo);
			htSyn.Add("@newtitle", tab_newModel.newTitle);
			htSyn.Add("@newkey", tab_newModel.newKey);
			htSyn.Add("@newdescription", tab_newModel.newDescription);
			htSyn.Add("@newname", tab_newModel.newName);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="nid">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int nid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [tab_new] where nid=@nid");
			htSyn.Add("nid", nid);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="tab_new">实体</param>
		/// <returns></returns>
		public bool Insert(tab_new tab_newModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [tab_new]([createDate],[newInfo],[newTitle],[newKey],[newDescription],[newName]) values (@createdate,@newinfo,@newtitle,@newkey,@newdescription,@newname)");
			htSyn.Add("@createdate", tab_newModel.createDate);
			htSyn.Add("@newinfo", tab_newModel.newInfo);
			htSyn.Add("@newtitle", tab_newModel.newTitle);
			htSyn.Add("@newkey", tab_newModel.newKey);
			htSyn.Add("@newdescription", tab_newModel.newDescription);
			htSyn.Add("@newname", tab_newModel.newName);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<tab_new> gettab_newBySql(string strSql)
		{
			IList<tab_new> list = new List<tab_new>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					tab_new tab_newModel = new tab_new();
					tab_newModel.createDate= Convert.ToDateTime(dr["createDate"]);
					tab_newModel.nid= Convert.ToInt32(dr["nid"]);
					tab_newModel.newInfo=dr["newInfo"].ToString();
					tab_newModel.newTitle=dr["newTitle"].ToString();
					tab_newModel.newKey=dr["newKey"].ToString();
					tab_newModel.newDescription=dr["newDescription"].ToString();
					tab_newModel.newName=dr["newName"].ToString();
					list.Add(tab_newModel);
				}
			}
			return list;
		}
		#endregion
	}
}
